package com.gwtcx.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.gwtcx.client.smartgwt.theme.AquaClientBundle;
import com.gwtcx.client.util.I18nUtil;

public class GwtCxEntryPoint implements EntryPoint {
	public static final String AQUA_THEME = "aqua";
	private static String theme= AQUA_THEME;
	private static boolean initialised = false;
	  
	@Override
	public void onModuleLoad() {
		if (! initialised) {
		      I18nUtil.init();
		      setTheme(AQUA_THEME);
		      initialised = true;
		}
	}
	
	public static String getRelativeURL(String url) {
	    // .html files are located relative to the applications host page
	    // e.g. /modulename/ModuleName.html not
	    //      /modulename/modulename/hosted.html
		return GWT.getHostPageBaseURL() + url;
	}

	public static String getTheme() {
		return theme;
	}

	public void setTheme(String theme) {
	    if (theme.equalsIgnoreCase(AQUA_THEME)) {
	      AquaClientBundle.INSTANCE.css().ensureInjected();
	      theme = AQUA_THEME;
	    }
	}

	  /**
	   * URL to the themed resource.
	   *
	   * For example, "/toolbar/new.png"
	   *
	   * @param  value the resource name
	   * @return       the resource url
	   */
	  public static String getThemedResource(String resource) {
	    return "../" + GWT.getModuleName() + "/gwtcx/skins/" + getTheme() + "/images/" + resource;
	  }

	  public static native String encodeBase64(String string) /*-{ return $wnd.btoa(string); }-*/;

	  public static native String decodeBase64(String string) /*-{ return $wnd.atob(string); }-*/;
}
